package com.asia5b.a5mall.middleware.logic.viewmodel.order

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.model.bean.OrderListType
import com.asia5b.a5mall.middleware.utils.showMessage
import io.reactivex.Observable
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/1/14.
 */
class OrderListFragmentVM : SimpleViewModel() {
    private var page = 1
    private var sumPage = 1000


    /**
     * @param refresh 0,刚进入界面刷新  1:用户手动刷新 2.用户加载更多
     * @param type 当前订单类型
     * @param house_id 当前仓库
     * @param 搜索关键字
     * @param 在某一种订单类型中搜索
     */
    fun getDatas(refresh: Int, type: OrderListType, house_id: Int? = null, goods_name: String? = null) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) {
            dismissLoadingBar()
            return
        }

        if (type == OrderListType.WAIT_REFUND) {//退货售后是单独的
            getRefundDatas(refresh)
            return
        }

        var observable = orderApi.getOrderList(goods_name, type.order_status, house_id, page)

        if (type == OrderListType.APPLY_PACKAGING) {//如果申请打包没有仓库id,直接返回空数据
            if (house_id == null || house_id!! <= 0) {
                val resultModel = ResultModel<ArrayList<OrderDetailModel>>()
                observable = Observable.just(resultModel)
            }
        }
        val netParams = SimpleNetParams(isShowLoading = (refresh == 0))
        netParams.requestTag = "getDatas"
        baseSubscribe(observable, netParams, Consumer { resultModel ->
            sumPage = resultModel.sumPage
            page = resultModel.page + 1
//            providerView().getDatasResult(resultModel.data)
            getDatasResultLiveData.value = resultModel
        })
    }

    val getDatasResultLiveData = MutableLiveData<ResultModel<*>>()

    /**
     * 退款售后
     */
    fun getRefundDatas(refresh: Int) {
        val observable = afterSaleApi.getRefundsList(page, 10)

        baseSubscribe(observable, SimpleNetParams(isShowLoading = (refresh == 0)), Consumer { resultModel ->
            sumPage = resultModel.sumPage
            page = resultModel.page + 1
            getDatasResultLiveData.value=resultModel
        })
    }

    val rogResultLiveData = MutableLiveData<Boolean>()
    val cancalResultLiveData = MutableLiveData<Boolean>()
    val getTradeSerialNoResultLiveData = MutableLiveData<Array<Any>>()
    val cancelRefundResultLiveData = MutableLiveData<String>()

    /**
     * @Note   确认收货
     * @param  orderSn 订单类别
     */
    fun rog(orderSn: String) {
        baseSubscribe(orderApi.rog(orderSn), Consumer {
            showMessage(R.string.确认收货成功)
            rogResultLiveData.value = true
        })
    }

    /**
     * @Note   取消订单
     * @param  orderSn 订单类别
     */
    fun cancel(orderSn: String, reason: String) {
        baseSubscribe(orderApi.cancel(orderSn, reason), Consumer {
            showMessage(R.string.取消成功)
//            providerView().cancalResult()
            cancalResultLiveData.value = true
        })
    }

    /**
     * 获取支付序列号
     */
    fun getTradeSerialNo(item: OrderDetailModel) {
        val observable = tradeApi.getTradeSerialNo(item.sn)
                .map { tradeSerialNoResult ->
                    tradeSerialNoResult.string()
                }
        baseSubscribe(observable, Consumer {
//            providerView().getTradeSerialNoResult(it, item)
            getTradeSerialNoResultLiveData.value = arrayOf(it, item)
        })
    }

    /**
     * 取消售后
     */
    fun cancelRefund(orderSn: String) {
        baseSubscribe(afterSaleApi.cancelRefund(orderSn), Consumer {
            showMessage(R.string.取消成功)
//            providerView().cancelRefundResult(orderSn)
            cancelRefundResultLiveData.value = orderSn
        })
    }

}